﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ExcelHelper
{
    public class ExcelToData
    {
        public static List<T> GetData<T>(string excelFile,string sheetName=null,string stopString="操作") where T:new ()
        {
            //
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            try
            {
                //表名
                if (string.IsNullOrWhiteSpace(sheetName))
                {
                    sheetName = typeof(T).Name;
                }

                using (var package = new ExcelPackage(new FileInfo(excelFile)))
                {
                    var sheet = package.Workbook.Worksheets[sheetName];
                    if (sheet != null)
                    {
                        //先读出表头字段
                        List<string> headers = new List<string>();
                        int col = 1;
                        while (true)
                        {
                            var val=sheet.Cells[1, col].Value;
                            if (val == null || val.ToString() == stopString)
                            {
                                break;
                            }
                            headers.Add(val.ToString());
                            col++;
                        }

                        //读出数据                        
                        int colCount = headers.Count;
                        if (colCount > 0)
                        {
                            List<T> data = new List<T>();
                            var typeItem = typeof(T);
                            int row = 2;
                            while (true)
                            {
                                T item = new T();
                                bool stop = false;
                                for (col = 1; col <= colCount; col++)
                                {

                                    var val = sheet.Cells[row, col].Value;
                                    if (val == null)
                                    {
                                        if (col == 1)//首列表示无数据了
                                        {
                                            stop = true;
                                            break;
                                        }                                        
                                    }
                                    else
                                    {
                                        var colType=typeItem.GetProperty(headers[col - 1]);
                                        if (colType != null)
                                        {
                                            colType.SetValue(item, StringParser.Parse(colType.PropertyType, val.ToString()));
                                        }
                                    }
                                }

                                if (stop)
                                {
                                    break;
                                }

                                data.Add(item);
                                //行增加
                                row++;
                            }

                            return data;


                        }

                        
                    }
                }

            }
            catch(Exception ex)
            {
                return null;
            }



            return null;
        }
    }
}
